package sq.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import sq.dao.Food;
import sq.dao.Orders;
import sq.dao.User;
import sq.service.OrdersService;
import sq.service.UserService;
import sq.vo.BaseRespVO;

import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: sunquan
 * @Date: 2018/5/8
 * @Time: 12:39
 */
@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping(value = "/user/all", method = RequestMethod.POST)
    @ResponseBody
    public BaseRespVO findAllUsers() {
        BaseRespVO baseRespVO = new BaseRespVO();
        ArrayList<User> userList = null;
        userList = (ArrayList<User>) userService.findAllUsers();
        baseRespVO.setCode("0");
        baseRespVO.setMsg("查询成功");
        baseRespVO.setData(userList);
        return baseRespVO;
    }
    /**
     * 查询用户
     *
     * @return
     */
    @RequestMapping(value = "/user/show/result", method = RequestMethod.POST)
    @ResponseBody
    public BaseRespVO queryUserBySearchBar(String userInfo) {
        BaseRespVO baseRespVO = new BaseRespVO();
        ArrayList<User> list = (ArrayList<User>) userService.findSearchUser(userInfo,userInfo);
        if (list.isEmpty()) {
            baseRespVO.setCode("1");
            baseRespVO.setMsg("没有文件查询失败");
        } else {
            baseRespVO.setCode("0");
            baseRespVO.setData(list);
            baseRespVO.setMsg("查询成功");
        }
        return baseRespVO;
    }

    /**
     * 查询特定用户
     *
     * @return
     */
    @RequestMapping(value = "/user/info", method = RequestMethod.POST)
    @ResponseBody
    public BaseRespVO queryUser(Integer userId) {
        BaseRespVO baseRespVO = new BaseRespVO();
        User user = userService.findUser(userId);
        if (user==null) {
            baseRespVO.setCode("1");
            baseRespVO.setMsg("没有文件查询失败");
        } else {
            baseRespVO.setCode("0");
            baseRespVO.setData(user);
            baseRespVO.setMsg("查询成功");
        }
        return baseRespVO;
    }


    /**
     * 更新用户
     *
     * @return
     */
    @RequestMapping(value = "/user/update", method = RequestMethod.POST)
    @ResponseBody
    public BaseRespVO updateUser(Integer userId,String username,String password) {
        BaseRespVO baseRespVO = new BaseRespVO();
        userService.updateUser(userId,username,password);
            baseRespVO.setCode("0");
            baseRespVO.setMsg("更新成功");

        return baseRespVO;
    }
    /**
     * 注销用户
     *
     * @return
     */
    @RequestMapping(value = "/user/delete", method = RequestMethod.POST)
    @ResponseBody
    public BaseRespVO deleteUser(Integer userId) {
        BaseRespVO baseRespVO = new BaseRespVO();
        userService.deleteUser(userId);
        baseRespVO.setCode("0");
        baseRespVO.setMsg("删除成功");

        return baseRespVO;
    }
}
